#include "VInputListWidget.h"
#include <QTimer>
#include <QDebug>

VInputListWidget::VInputListWidget(QWidget *parent) :
    QListWidget(parent),
    _timer(this)
{
    QObject::connect(&_timer, &QTimer::timeout, this, &VInputListWidget::on_hide);
    QObject::connect(this, &VInputListWidget::doubleClicked, this, &VInputListWidget::on_doubleClicked);
}

void VInputListWidget::hide()
{
    _timer.start(100);
}

void VInputListWidget::show()
{
    _timer.stop();
    QListWidget::show();
}

void VInputListWidget::on_hide()
{
    _timer.stop();
//    qDebug() << "on_hide";

    if(this->hasFocus() == false)
    {
//        qDebug() << "not focus";
        QListWidget::hide();
    }
}

void VInputListWidget::on_doubleClicked()
{
    auto item = this->currentItem();
    if(item == nullptr)
    {
        return;
    }

    VWords* ptr = (VWords*)(item->data(Qt::UserRole).value<void*>());
    emit signal_input_word(new VWords(ptr));
    QListWidget::hide();
}
